package com.xiaoyang.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoyang.ssyx.acl.mapper.RoleMapper;
import com.xiaoyang.ssyx.acl.service.AdminRoleService;
import com.xiaoyang.ssyx.acl.service.RoleService;
import com.xiaoyang.ssyx.model.acl.AdminRole;
import com.xiaoyang.ssyx.model.acl.Role;
import com.xiaoyang.ssyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @AUTHOR XiaoYang
 * @DATE 2023/6/13-19:32
 * @DESCRIPTION TODO
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        //获取条件值
        String roleName = roleQueryVo.getRoleName();
        //创建mp条件对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //判断条件值是否为空，不为空封装查询条件
        if (!StringUtils.isEmpty(roleName)) {
            wrapper.like(Role::getRoleName,roleName);
        }
        //调用方法实现条件分页查询
        IPage<Role> roleIPage = baseMapper.selectPage(pageParam, wrapper);
        //返回分页对象
        return roleIPage;
    }

    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //1、查询所有角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //2、根据用户id查询用户分配角色列表
        //2.1、根据用户id查询 用户角色关系表 admin_role 查询到分配的id List<AdminRole>
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);

        //2.2、通过第一步返回的集合 获取到所有角色id的列表List<Long>
        List<Long> roleIdsList = adminRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());

        //2.3、创建一个新的list集合，用户存储用户配置角色
        List<Role> assignRoles = new ArrayList<>();

        //2.4、遍历所有角色列表allRolesList,得到每个角色
        for (Role role: allRolesList) {
            //判断
            if (roleIdsList.contains(role.getId())){
                assignRoles.add(role);
            }
        }

        //2.5、判断所有角色里面是否包含已经分配角色id,封装到2.3里面新的ist集合

        Map<String, Object> result = new HashMap<>();
        //所有角色
        result.put("allRolesList", allRolesList);
        //用户已经分配的角色
        result.put("assignRoles", assignRoles);

        return result;
    }

    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        //1、删除用户己经分配过的角色数据
        //根据用户id删除admin_role表里对应的数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        adminRoleService.remove(wrapper);
        //2、重新分配
        //遍历多个角色id，得到每个角色id，拿着每个角色id + 用户id 添加用户角色关系表admin_role
        List<AdminRole> adminRoleList = new ArrayList<>();
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoleList.add(adminRole);
        }
        adminRoleService.saveBatch(adminRoleList);
    }
}
